package com.joker.warehouse.service.impl;

import com.joker.common.core.utils.bean.BeanUtils;
import com.joker.common.security.utils.SecurityUtils;
import com.joker.warehouse.domain.Detail;
import com.joker.warehouse.domain.PmsSupplier;
import com.joker.warehouse.domain.Purchase;
import com.joker.warehouse.domain.dto.PurchaseDTO;
import com.joker.warehouse.domain.dto.PurchaseDetailDTO;
import com.joker.warehouse.domain.vo.PurchaseVO;
import com.joker.warehouse.mapper.PurchaseMapper;
import com.joker.warehouse.service.IDetailService;
import com.joker.warehouse.service.INeedService;
import com.joker.warehouse.service.IPmsSupplierService;
import com.joker.warehouse.service.IPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lemon
 * @date 2025/01/13 21:30
 **/
@Service
public class PurchaseServiceImpl implements IPurchaseService {

    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private IDetailService detailService;
    @Autowired
    private IPmsSupplierService pmsSupplierService;
    @Autowired
    private INeedService needService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(PurchaseDTO purchaseDTO) {
        //插入采购单
        Purchase purchase = new Purchase();
        BeanUtils.copyProperties(purchaseDTO,purchase);
        purchase.setPurchaser(SecurityUtils.getUserId());
        purchaseMapper.insert(purchase);

        //插入采购明细
        List<PurchaseDetailDTO> needs = purchaseDTO.getNeeds();
        List<Detail> collect = needs.stream().map(detailDTO -> {
            Detail detail = new Detail();
            BeanUtils.copyProperties(detailDTO, detail);
            detail.setPurchaseId(purchase.getId());
            return detail;
        }).collect(Collectors.toList());
        detailService.saveDetail(collect);

        //修改采购需状态
        List<Long> ids = needs.stream().map(PurchaseDetailDTO::getId).collect(Collectors.toList());
        needService.complete(ids);
    }

    @Override
    public void check(Long purchaseId, Integer status) {
        Purchase purchase = new Purchase();
        purchase.setId(purchaseId);
        purchase.setStatus(status);
        purchase.setAuditor(SecurityUtils.getUserId());
        purchase.setAuditTime(new Date());

        purchaseMapper.updateCheckStatus(purchase);
    }

    @Override
    public List<PurchaseVO> selectPmsPurchaseList(Purchase purchase) {
        List<Purchase> purchases = purchaseMapper.selectPmsPurchaseList(purchase);
        return purchase2VO(purchases);
    }

    protected List<PurchaseVO> purchase2VO(List<Purchase> purchaseList){
        return purchaseList.stream().map(pur -> {
            PurchaseVO purchaseVO = new PurchaseVO();
            BeanUtils.copyProperties(pur,purchaseVO);
            PmsSupplier pmsSupplier = pmsSupplierService.selectPmsSupplierById(pur.getSupplierId());
            purchaseVO.setSupplier(pmsSupplier.getSupplier());
            return purchaseVO;
        }).collect(Collectors.toList());
    }
}
